var express = require('express');
var router = express.Router();
var User = require('../model/User');
var Response = require('../util/ResponseUtil');

/* list user for testing */
router.get('/', function (req, res, next) {
    User.find({}, function (err, users) {
        console.log(users);
        res.json(users);
    });
});


/* login */
router.post('/login', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    User.login(username, password, function (err, user) {

        if (err) {
            Response.ERROR(res, err);
            return;
        }

        Response.OK(res, user);
    });
});


/* register */
router.post('/register', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    User.register(username, password, function (err) {
        if (err) {
            console.log(err);
            Response.ERROR(res);
            return;
        }

        Response.OK(res);
    });

});

module.exports = router;
